﻿using System;
using Payton.WP7.Common.Model;
using System.Collections.Generic;
using Hammock;
using Hammock.Authentication.OAuth;
using Hammock.Web;
using Hammock.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Linq;
using Newtonsoft.Json.Linq;
using System.Globalization;

namespace Payton.WP7.Common.Sina
{
    public class SinaStatusService : StatusServiceBase
    {
        protected override IList<SNSStatus> Mapping(string res, SNSResult result)
        {
            List<SNSStatus> statuses = null;
            try
            {
                if (res.Contains("error"))
                {
                    //TODO HANDLE ERROR:
                    // Refresh / Notify user to refresh
                    return statuses;
                }
                string content = "";
                if (!res.StartsWith("["))
                {
                    content = string.Format("[{0}]", res);
                }
                else
                    content = res;
                JObject o = JObject.Parse("{\"statuses\":" + content + "}");

                statuses = o.SelectToken("statuses").Select(
                    s => factory.GetMapping<JToken, SNSStatus>().Map(s)
                    ).ToList();
            }
            catch (Exception)
            {
                throw;
            }

            return statuses;
        }


        protected override SNSStatus MappingSingle(string res, SNSResult result)
        {
            SNSStatus status = null;
            try
            {
                var content = res;
                if (content.Contains("error"))
                {
                    //TODO HANDLE ERROR:
                    // Refresh / Notify user to refresh
                    return status;
                }
                JObject o = JObject.Parse(content);

                status = factory.GetMapping<JToken, SNSStatus>().Map(o);
            }
            catch (Exception)
            {
                throw;
            }
            return status;
            //return base.MappingSingle(res);
        }

        protected override string[] GetParamtersList(SNSType type)
        {
            string[] list;
            switch (type)
            {
                case SNSType.HomeStatus:
                    list = new string[] { 
                        SNSParametersConstants.SINCE_ID,
                        SNSParametersConstants.MAX_ID ,
                        SNSParametersConstants.COUNT,
                        SNSParametersConstants.PAGE,
                        SNSParametersConstants.BASE_APP,
                        SNSParametersConstants.FEATURE
                    };
                    break;
                case SNSType.UserStatus:
                    list = new string[] { 
                        SNSParametersConstants.USER_ID,
                        SNSParametersConstants.SINCE_ID,
                        SNSParametersConstants.MAX_ID ,
                        SNSParametersConstants.COUNT,
                        SNSParametersConstants.PAGE,
                        SNSParametersConstants.BASE_APP,
                        SNSParametersConstants.FEATURE
                    };
                    break;
                case SNSType.MentionsStatus:
                    list = new string[] { 
                        SNSParametersConstants.SINCE_ID,
                        SNSParametersConstants.MAX_ID ,
                        SNSParametersConstants.COUNT,
                        SNSParametersConstants.PAGE
                    };
                    break;
                case SNSType.SearchStatus:
                    list = new string[] { 
                        SNSParametersConstants.TREND_NAME
                    };
                    break;
                case SNSType.PublicStatus:
                    list = new string[] { 
                        SNSParametersConstants.COUNT,
                        SNSParametersConstants.BASE_APP
                    };
                    break;
                case SNSType.AddStatus:
                    list = new string[] { 
                        SNSParametersConstants.STATUS_TEXT
                    };
                    break;
                case SNSType.RepostStatus:
                    list = new string[] 
                    { 
                        SNSParametersConstants.ID,
                        SNSParametersConstants.STATUS_TEXT,
                        SNSParametersConstants.IS_COMMENT };
                    break;
                case SNSType.CommentsToMe:
                    list = new string[] { 
                        SNSParametersConstants.SINCE_ID,
                        SNSParametersConstants.MAX_ID ,
                        SNSParametersConstants.COUNT,
                        SNSParametersConstants.PAGE,
                    };
                    break;
                case SNSType.CommentsByMe:
                    list = new string[] { 
                        SNSParametersConstants.SINCE_ID,
                        SNSParametersConstants.MAX_ID ,
                        SNSParametersConstants.COUNT,
                        SNSParametersConstants.PAGE
                    };
                    break;
                case SNSType.CommentsByID:
                    list = new string[] { 
                        SNSParametersConstants.ID,
                        SNSParametersConstants.SINCE_ID,
                        SNSParametersConstants.MAX_ID ,
                        SNSParametersConstants.COUNT,
                        SNSParametersConstants.PAGE
                    };
                    break;
                case SNSType.AddComment:
                    list = new string[]
                    { 
                        SNSParametersConstants.ID,
                        SNSParametersConstants.COMMENT,
                        SNSParametersConstants.CID,
                        SNSParametersConstants.WITHOUT_MENTION,
                        SNSParametersConstants.COMMENT_ORI
                    };
                    break;
                case SNSType.DeleteComment:
                    list = new string[] { 
                        //SNSParametersConstants.COUNT,
                        //SNSParametersConstants.BASE_APP
                    };
                    break;
                case SNSType.Upload:
                    list = new string[] { 
                        SNSParametersConstants.STATUS_TEXT,
                        SNSParametersConstants.PIC,
                        SNSParametersConstants.LAT,
                        SNSParametersConstants.LONG
                        //SNSParametersConstants.BASE_APP
                    };
                    break;
         
                default:
                    list = new string[] 
                    { 
                    };
                    break;
            }
            return list;
        }

        protected override int ValidateResponse(RestResponse res, out string msg)
        {
            if (res.Content.StartsWith("<"))
            {
                msg = res.Content;
                return 0;
            }

            if (string.IsNullOrEmpty(res.Content))
            {
                msg = "no data";
                return 99;
            }

            if (res.Content.Contains("error"))
            {
                msg = res.Content;
                //TO-DO HANDLE ERROR
                return 0;
            }
            msg = "ok";
            
            return 1;
        }

        protected override void Initialize()
        {
            //throw new NotImplementedException();
            STATUS_HOME_TIMELINE = "/statuses/friends_timeline.json";
            STATUS_MENTIONS_TIMELINE = "/statuses/mentions.json";
            STATUS_SEARCH_TIMELINE = "/trends/statuses.json";
            STATUS_USER_TIMELINE = "/statuses/user_timeline.json";
            STATUS_UPDATE = "/statuses/update.json";
            STATUS_REPOST = "/statuses/repost.json";
            STATUS_REPLY = "/statuses/comment.json";
            COMMENT_BY_ME = "/statuses/comments_by_me.json";
            COMMENT_TO_ME = "/statuses/comments_to_me.json";
            COMMENT_BY_ID = "/statuses/comments.json";
            COMMENT_ADD = "/statuses/comment.json";
            COMMENT_DELETE = "/statuses/comment_destroy/:id.json";
            STATUS_UPLOAD = "/statuses/upload.json";
            STATUS_COUNTS = "/statuses/upload.json";
        }

        public override void GetTimeline(SNSRequest snsRequest, Action<IList<SNSStatus>, SNSResult> action)
        {
            if (snsRequest.Type == SNSType.SearchStatus && snsRequest.Parameters.FirstOrDefault(p => p.Name == SNSParametersConstants.MAX_ID)!=null)
            {
                if (action != null)
                    action(null, new SNSResult() { IsSuccess = false, Message="新浪不支持搜索分页显示" });
                return;
            }
            base.GetTimeline(snsRequest, action);
        }
    }
}
